﻿@{
    ViewData["Title"] = "Home Page";
}


<input type="text" id="loginName" />
<input type="text" id="loginPwd" />
<button onclick="login()">登录</button>
<button onclick="logout()">登出</button>
<button onclick="getUserInfo()">获取用户信息</button>
@section Scripts{
    <script src="~/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        //$.ajax({
        //    type: "get",
        //    url: "/api/User/GetUserInfo?loginName=zhangSan",
        //    dataType: "json",
        //    success: function (data) {
        //        //success比done先执行
        //    }
        //}).done(function (data) {
        //    console.log(data);
        //});

        //$.ajax({
        //    type: "post",
        //    url: "/api/Student?basic1=1&basic2=guo",//?basic1=9&basic2=guo
        //    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        //    data: {
        //        id: 12,
        //        name: "guo",
        //        basic3: "basic3",
        //    },
        //}).done(function (data) {
        //    alert("done(), data=" + data);
        //}).fail(function (err) {
        //    alert("fail(), err=" + err);
        //});

        //$.ajax({
        //    type: "get",
        //    url: "/api/User/Message",
        //    data: {
        //        Id: 12,
        //        Name: "name"
        //    }
        //}).done(function (data) {
        //    console.log(data);
        //}).fail(function (err) {
        //    alert("fail(), err=" + err);
        //});

        //规定ajax请求即将发送时运行的函数
        $(document).ajaxSend(function (e, jqxhr, opt) {
            //统一为ajax请求添加Header
            jqxhr.setRequestHeader("Authorization", "Bearer " + sessionStorage.getItem("token"));
        });

        //规定ajax请求成功完成时运行的函数
        $(document).ajaxSuccess(function (event, jqxhr, opt) {
            if (jqxhr.responseJSON.Success) {
                handleStatusCode(jqxhr.responseJSON.Code);
            }
        });

        function login() {
            let name = $.trim($("#loginName").val());
            let pwd = $.trim($("#loginPwd").val());
            $.ajax({
                type: "GET",
                url: "/api/User/GetToken",
                contentType: "application/json",
                data: {
                    data: JSON.stringify({
                        loginName: name,
                        password: pwd
                    })
                }
            }).done(function (data) {
                if (data.Success) {
                    sessionStorage.setItem("token", data.Data);
                } else {
                    alert(data.ErrorMsg);
                }
            });
        }

        function logout() {
            sessionStorage.removeItem("token");
        }

        function getUserInfo() {
            $.ajax({
                type: "get",
                url: "/api/User/GetUserInfo"
            }).done(function (data) {
                if (data.Success) {
                    console.log(data.Data);
                } else {
                    alert(data.ErrorMsg);
                }
            });
        }

        function handleStatusCode(statusCode) {
            if (statusCode == 9001) {
                $.ajax({
                    type: "get",
                    url: "/api/User/RefreshToken"
                }).done(function (data) {
                    if (data.Success) {
                        sessionStorage.setItem("token", data.Data);
                    } else {
                        alert(data.ErrorMsg);
                    }
                }).fail(function (err) {
                    alert("fail(), err=" + err);
                });
            }
        }
    </script>
}
